#include <cstdio>
#include <cstring>

#define MAXN 100

struct mat{
    int n,m;
    __int64 data[MAXN][MAXN];
};

mat a, b;
__int64 n, m, c[ 71 ][ 71 ];
int x;

int mul(mat& c,const mat& a,const mat& b){
    int i,j,k;
    if (a.m!=b.n)
        return 0;
    c.n=a.n,c.m=b.m;
    for (i=0;i<c.n;i++)
        for (j=0;j<c.m;j++)
            for (c.data[i][j]=k=0;k<a.m;k++)
            {
                c.data[i][j]+=a.data[i][k]*b.data[k][j];
                c.data[i][j]%=m;
            }
    return 1;
}

void init( )
{
    int i, j;
    a.n = 1; a.m = x + 2;
    for ( i = 0; i < a.m; i++ ) a.data[ 0 ][ i ] = 0;
    a.data[ 0 ][ 1 ] = 1;
    b.n = b.m = x + 2;
    memset( b.data, 0, sizeof( b.data ) );
    b.data[ 0 ][ 0 ] = 1;
    for ( i = 1; i < b.n; i++ )
        b.data[ i ][ 0 ] = x * c[ x ][ i - 1 ] % m;
    for ( i = 1; i < b.m; i++ )
        for ( j = 1; j <= i; j++ )
            b.data[ j ][ i ] = x * c[ i - 1 ][ j - 1 ] % m;
}

void work( )
{
    int i, j;
    __int64 p = 1;
    mat c, t;
    c.n = c.m = b.n;
    memset( c.data, 0, sizeof( c.data ) );
    for ( i = 0; i < c.n; i++ )
        c.data[ i ][ i ] = 1;
    while ( p <= n ) p *= 2;
    p /= 2;
    while ( p )
    {
        mul( t, c, c );
        c = t;
        if ( p & n )
        {
            mul( t, c, b );
            c = t;
        }
        p /= 2;
    }
    mul( t, a, c );
    printf("%I64d\n", t.data[ 0 ][ 0 ]);
}

int main( )
{
    int i, j;
    for ( i = 0; i <= 50; i++ )
    {
        c[ i ][ 0 ] = 1;
        for ( j = 1; j <= i; j++ )
            c[ i ][ j ] = c[ i ][ j - 1 ] * ( i - j + 1 ) / j;
    }
    while ( scanf("%I64d%d%I64d", &n, &x, &m) )
    {
        if ( n == -1 && x == -1 && m == -1 )
            break;
        init( );
        work( );
    }
    return 0;
}
